﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Bamboo.Helpers;
namespace Gama.Extension
{
    public static partial class HtmlHelperExtensions
    {
        public static TextAreaBuilder<TModel> TextAreaEx<TModel>(this HtmlHelper htmlHelper, Expression<Func<TModel, object>> expression)
        {
            return new TextAreaBuilder<TModel>(htmlHelper, expression);
        }
    }

    public class TextAreaBuilder<TModel> : HtmlInputElementBuilder<TModel>
    {
        internal TextAreaBuilder(HtmlHelper htmlHelper, Expression<Func<TModel, object>> expression)
            : base(htmlHelper, expression)
        {
            // default
            this.value = expression.Compile()((TModel)htmlHelper.ViewData.Model);            
        }

        protected int rows = 2;
        protected int cols = 20;

        public TextAreaBuilder<TModel> Rows(int rows)
        {
            this.rows = rows;
            return this;
        }

        public TextAreaBuilder<TModel> Cols(int cols)
        {
            this.cols = cols;
            return this;
        }

        protected override string BuildCore()
        {
            if (this.rows == 0 || this.cols == 0)
            {
                return this.htmlHelper.TextArea(htmlHelper.BuildBindingPath(this.expression), this.value.ToString(), this.htmlAttributes).ToHtmlString();
            }

            return this.htmlHelper.TextArea(htmlHelper.BuildBindingPath(this.expression), this.value.ToStringEx(), 
                this.rows, this.cols,
                this.htmlAttributes).ToHtmlString();            
        }
    }
}
